<template>
  <div style="height: 100%;display: flex;flex-direction: column">
    <el-row>
      <el-card style="width: 100%">
        密码管理
      </el-card>
    </el-row>

    <el-row style="margin-top: 10px;flex: 1;display:flex;align-items: center;justify-content: center;">
      <el-card style="flex: 1;display: flex;justify-content: center">
        <el-form label-width="120px" :model="state.userInfo" ref="formRef" :rules="rules">
          <el-form-item label="用户ID" prop="userId">
            {{userStore.userInfo.userId}}
          </el-form-item>
          <el-form-item label="用户名" prop="username">
            {{userStore.userInfo.username}}
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input type="password" show-password v-model="state.userInfo.password"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input type="confirmPassword" show-password v-model="state.userInfo.confirmPassword"></el-input>
          </el-form-item>
          <el-form-item label="加密方式" prop="encryptedType">
            <el-radio-group  v-model="state.userInfo.encryptedType">
              <el-radio label="1">BCrypt</el-radio>
              <el-radio label="2">MD5</el-radio>
              <el-radio label="3">不加密</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
              <div style="width: 100%;display: flex;justify-content: space-between">
                <el-button :icon="Edit" type="primary" @click="toUpdate">修改</el-button>
                <el-button :icon="Delete" type="warning">清空</el-button>
              </div>
          </el-form-item>
        </el-form>
      </el-card>
    </el-row>
  </div>

  <GuiguLoading :is-loading="state.isLoading" text="正在修改请稍等..."></GuiguLoading>
</template>

<script setup>
  import useUserStore from "@/store/modules/user.js";
  import {reactive, ref} from "vue";
  import * as validator from "@/api/util/validator.js";

  import {Edit,Delete} from "@element-plus/icons-vue";
  import {updatePassword} from "@/api/backend/user/user.js";
  import GuiguLoading from "@/components/GuiguLoading/index.vue";
  import {ElMessage} from "element-plus";
  const userStore=useUserStore()

  const state=reactive({
    isLoading:false,
    userInfo:{userId:userStore.userInfo.userId,password:"",confirmPassword:"",encryptedType:userStore.userInfo.encryptedType}
  })

  const rules ={

    password:[
      {required:true,message:"密码不能为空",trigger:'blur'},
      {min:6, max: 20, message: "长度在6-20之间", trigger: 'blur'}
    ],
    confirmPassword:[
      {required:true,message:"确认密码与密码不一致",trigger:'change',validator:validator.validateConfirmPassword(state.userInfo)}
    ]
  }
  const formRef=ref()

  const toUpdate=()=>{
      state.isLoading=true
      updatePassword(state.userInfo).finally(()=>{
        state.isLoading=false
      }).then(res=>{
         ElMessage({
           type:"success",
           message:"修改成功"
         })
      }).catch(error=>{
        ElMessage({
          type:"error",
          message:error.msg
        })
      })
  }
</script>

<style scoped>

</style>